﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace MultyLanguage
{
    public partial class Pannel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //MultyLanguage.XMLUltility.SetOrEditValue("item","t","valgsag cua no","innerHTML",@"C:\Users\Gia Bang\Desktop\intership\MultyLanguage\MultyLanguage\MultyLanguage\language\vi_VI\Pannel.aspx.xml");
            // find all resources on server
            GetAllResources(Server.MapPath("~"));

            // get all available languages
            GetAvailableLanguages(Server.MapPath("~/language"));

            txtValue.Text = "";
            //if(Request.Params["lstResource"] != null && Request.Params["lstResource"] .ToString().Length >0)
            //{
               
            //}
        }

        private void GetAllResources(string path)
        {
            //lstResource.Items.Clear();
            if (lstResource.Items.Count == 0)
            {
                string[] files = Directory.GetFiles(path);
                foreach (string f in files)
                {
                    if (f.EndsWith(".aspx") || f.EndsWith(".ascx"))
                    {
                        string[] farr = f.Split(new char[] { '\\' });
                        string t = farr[farr.Length - 1];
                        lstResource.Items.Add(t);
                        
                    }
                }

                string[] subdirs = Directory.GetDirectories(path);
                foreach (string d in subdirs)
                {
                    files = Directory.GetFiles(d);
                    string[] darr = d.Split(new char[] { '\\' });

                    foreach (string f in files)
                    {
                        if (f.EndsWith(".aspx") || f.EndsWith(".ascx"))
                        {

                            string[] farr = f.Split(new char[] { '\\' });
                            string t = farr[farr.Length - 1];
                            lstResource.Items.Add(darr[darr.Length - 1] + "\\" + t);
                        }
                    }
                }

                lstResource.SelectedIndex = 0;
            }
        }

        private void GetAvailableLanguages(string path)
        {
            //languagesList.Items.Clear();
            if (languagesList.Items.Count == 0)
            {
                string[] languages = Directory.GetDirectories(path);
                foreach (string l in languages)
                {
                    languagesList.Items.Add(l.Substring(l.LastIndexOf("\\") + 1));
                    ListShowLanguage.Items.Add(l.Substring(l.LastIndexOf("\\") + 1));
                }
            }
        }


        protected void lstResource_TextChanged(object sender, EventArgs e)
        {

            string path = Server.MapPath("~/" + Request.Params["lstResource"]);
            string defaultpath = Server.MapPath("~/language/default/" + Request.Params["lstResource"]  + ".xml");
            if (File.Exists(defaultpath) == false)
            {
                XmlDocument xdoc = new XmlDocument();
                string xmlstring = "<language>" +

                                   "</language>";
                xdoc.LoadXml(xmlstring);
                xdoc.Save(defaultpath);

            }
            #region get id control
            controlsList.Items.Clear();
            string[] tags = NetUtil.getAllControl("asp:Button", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("Button." + tags[ta]);
                }
            }
            tags = NetUtil.getAllControl("asp:TextBox", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("TextBox." + tags[ta]);
                }
            }
            tags = NetUtil.getAllControl("asp:LinkButton", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("LinkButton." + tags[ta]);
                }
            }
            tags = NetUtil.getAllControl("asp:CheckBox", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("CheckBox." + tags[ta]);
                }

            }
            tags = NetUtil.getAllControl("asp:Label", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("Label." + tags[ta]);
                }
            }
            tags = NetUtil.getAllControl("asp:RadioButton", path, defaultpath);
            if (tags != null)
            {
                for (int ta = 0; ta < tags.Length; ta++)
                {
                    controlsList.Items.Add("RadioButton." + tags[ta]);
                }
            }
            string[] languages = Directory.GetDirectories(Server.MapPath("~/language"));
            foreach (string l in languages)
            {
                //languagesList.Items.Add(l.Substring(l.LastIndexOf("\\") + 1));
                string xmlpath = Server.MapPath("~/language/" + "/" + l.Substring(l.LastIndexOf("\\") + 1) + "/" + Request.Params["lstResource"] + ".xml");
                if (System.IO.File.Exists(xmlpath) == false)
                {
                    XmlDocument xdoc = new XmlDocument();
                    string xmlstring = "<language>" +

                                       "</language>";
                    xdoc.LoadXml(xmlstring);
                    xdoc.Save(xmlpath);
                }
            }
            #endregion;

        }

        protected void ListShowLanguage_TextChanged(object sender, EventArgs e)
        {
            Application["xmlpath"] =  Request.Params["ListShowLanguage"].Trim();
        }

        /*protected void Check(object sender, ServerValidateEventArgs e)
        {
            Button target = (Button)sender;
            if (target.Text.Contains("<"))
            {
                if (target.Text.Contains(">") || target.Text.Contains("!"))
                    e.IsValid = false;
            }
            else
                e.IsValid = true;
        }*/
    }
}
